const SimpleProgressWebpackPlugin = require('simple-progress-webpack-plugin');
const CracoLessPlugin = require('craco-less');
const dayjs = require('dayjs');
const packageJson = require('./package.json');

// 注入环境变量，需要以REACT_APP_前缀才生效
function addEnvHook(envs) {
  Object.entries(envs || {}).forEach(([key, value]) => {
    process.env[key] = value;
  });
}
addEnvHook({
  REACT_APP_NAME: packageJson.name,
  REACT_APP_BUILD_TIME: dayjs().format('YYYY-MM-DD hh:mm:ss'),
  REACT_APP_VERSION: packageJson.version,
});

module.exports = {
  devServer: {
    open: false,
    port: 8080,
    proxy: {
      '/api': {
        target: (() => {
          if (process.env.SERVER_PROXY_TYPE === 'MOCK') {
            return 'http://localhost:3001/';
          }
          return '';
        })(),
        changeOrigin: true,
        pathRewrite: {
          '^/api': '',
        },
      },
    },
  },
  webpack: {
    plugins: [new SimpleProgressWebpackPlugin()],
  },
  babel: {
    plugins: [
      [
        'import',
        { libraryName: 'antd', libraryDirectory: 'es', style: false },
        'antd',
      ],
    ],
  },
  eslint: {
    enable: false,
  },
  plugins: [
    {
      plugin: CracoLessPlugin,
      options: {
        lessLoaderOptions: {
          lessOptions: {
            javascriptEnabled: true,
          },
        },
      },
    },
  ],
};
